$(function(){
	// 加载列表
	loadShareFileList(parentId,shareUuid);
	
	$("ul li a[href$='shareMyList.jsp']").addClass("active");
	
	// 添加点击文件
	$("#shareMyList").on("click",".folderCls",function(){
		clickFolderFn($(this).attr("key"),$(this).attr("shareUuid"),isFolderFn($(this).attr("folderType")));
	});
	
	// 给目录导航条添加事件
	$("#buildCrumbs").on("click","a",function(){
		clickFolderFn($(this).attr("key"),$(this).attr("shareUuid"),true);
	});
	
	//添加下载 
	$("#shareMyList").on("click",".downupLoad",function(e){
		e.preventDefault();
		e.stopPropagation();			//阻止冒泡
		clickFolderFn($(this).attr("key"),"",false);
	});
	
	// 全部
	$("#shareMyFile").on("click",function(){
		window.location.href = ctx+"/jsp/cd/doc/shareMyList.jsp";
	});
	
	
	
	
	// 全选checkbox点击事件
	$('table th input:checkbox').on('click',function(){
		var p = this;
		$(p).closest('table').find('tr > td:first-child input:checkbox').each(function(){
			this.checked = p.checked;
		});
		
	});
});


/**
 * 是否为文件夹
 * @param type	类型
 */
function isFolderFn(type){
	return type=="40010001";
}


/**
 * 向页面插入分享文件给我的信息主页
 * @param data
 */
function renderFileList(data,shareUuid){
	var showListMtp = $('#showListMtp').html();
	Mustache.parse(showListMtp);
	var rendered = Mustache.render(showListMtp,{
			data: data,
			isfolder: function(){					//是否为文件夹
				return isFolderFn(this.folderType);
			},
			shareUuid: function(){
				if(shareUuid){
					return shareUuid;
				}
				return this.uuid;
			}
	});
	$("#shareMyList").html(rendered);
}



/**
 * 点击文件夹
 * @param uuid
 * @param shareUuid
 * @param folderType
 */
function clickFolderFn(uuid,shareUuid,isFolder){
	if(isFolder){								//打开文件夹
		window.location.href = ctx+"/jsp/cd/doc/shareMyList.jsp?parentId="+uuid+"&shareUuid="+shareUuid;
	}else{																	//下载
		Otod.file.docUpload.download(uuid);
	}
}




/**
 * 点击 目录 查询下级 目录信息
 * @param parentId	上级ID
 * @param shareUuid	分享级uuid
 */
function loadShareFileList(parentId,shareUuid){
	//用于存储导航条的名字和id

	Otod.ajax({
		url: ctx + '/cd/Share/queryForList.do',
		data:{parentId:parentId,shareFolderUuid:shareUuid},
		type:"GET",
		successFn : function(data){	
			var vo = data.cdDocFolderVo;
			buildCrumbs(vo,data.shareUuid);
			
			//显示列表信息
			renderFileList(data.childrenList,data.shareUuid);
		}
	});
}



/**
 * 生成面包屑
 * @param vo
 */
function buildCrumbs(vo,shareUuid){
	if(vo==null){
		return;
	}
	//分割路径的名字
	var pathName = vo.folderPathName;
	var pathNameArray = new Array();
	var pathIdArray = new Array();
	if(pathName){
		pathNameArray = pathName.split(",");
		pathIdArray = vo.folderPath.split(",");
	}
	pathNameArray.push(vo.folderName);
	
	pathIdArray.push(vo.uuid);
	//导航条的全部文件 变成超链接
	var skipFile = '<a href="#" class="allFile">全部文件</a>';
	var html = Mustache.render(skipFile);
	$("#skipFile").html(html);
	
	var startFlag = false;
	
	var pathSb = new StringBuffer();
	for(var i=0;i<pathIdArray.length;i++){
		if(shareUuid == pathIdArray[i]){
			startFlag = true;
		}
		if(!startFlag){
			continue;
		}
		pathSb.append('<i class="icon-fixed-width icon-angle-right dark"></i>');
		pathSb.append('<a href="#"  key="').append(pathIdArray[i]).append('" shareUuid="').append(shareUuid).append('">');							
		pathSb.append(pathNameArray[i]);
		pathSb.append('</a>');
	}
	$("#buildCrumbs").html(pathSb.toString());
}














Otod.file= {};
Otod.file.docUpload = {
	options: {
		url: Global.ctx + "/servlet/upload?type=interfaceManage&beanName=cdDocFileService",
		previewsContainer: '',
	    paramName: "file", // The name that will be used to transfer the file
	    maxFilesize: 1600, // MB
	    createImageThumbnails:false,
		addRemoveLinks : false,
		sending: function(file, xhr, formData){
			formData.append("parentId", parentId);
		},
		success: function(file, data, e) {
			loadFileList(parentId);
		},
		error: function(file, message){
			Otod.Msg.error(message.error);
		},
		clickable: true
	},
	init: function(dropzoneObj,previewsContainer,clickable){
		Otod.file.docUpload.options.clickable = clickable;
		Otod.file.docUpload.options.previewsContainer = previewsContainer;
		new Dropzone(dropzoneObj, 
				jQuery.extend({clickable:clickable,previewsContainer:previewsContainer}
					,Otod.file.docUpload.options));
	},
	download: function(id){
		var url = Global.ctx+"/servlet/upload?sname="+id+"&isBusid=true&type=commonManage";
		document.getElementById("_downloadFileFrame").src = url;
	}
};
